<template>
  <section class="webview fixHeight">
    <el-scrollbar style="height:100%">
      <el-form ref="form" :model="detail" label-width="100px" class="form">
        <el-form-item label="账号">
          <el-input v-model="detail.account" />
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="detail.address" />
        </el-form-item>
        <el-form-item label="服务范围">
          <el-input v-model="detail.serviceScope" />
        </el-form-item>
        <el-form-item label="房产公司">
          <el-select v-model="detail.enterpriseId"  placeholder="请选择">
            <el-option
              v-for="item in enterpriseIdList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="营业执照号">
          <el-input v-model="detail.license" />
        </el-form-item>
        <el-form-item label="门店logo">
          <div class="picList">
            <div v-for="(item,index) in fileList" class="item">
              <img class="pic" :src="item" alt>
              <i class="el-icon-delete delIcon hand" @click="handleRemove(index)" />
            </div>
          </div>
          <div v-if="fileList.length < 1" class="item itemNo">
            <el-upload
              :action="Is.sevenDomain"
              :multiple="false"
              name="file"
              :limit="1"
              :show-file-list="false"
              :before-upload="handleUpload"
              :on-success="handleSuccess"
            >
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，只能上传1张，且不超过10M</div>
              <div class="el-upload el-upload--picture-card">
                <i slot="default" class="el-icon-plus" />
              </div>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="门店名称">
          <el-input v-model="detail.name" />
        </el-form-item>
<!--        <el-form-item label="服务范围">-->
<!--          <el-input v-model="detail.serviceScope" />-->
<!--        </el-form-item>-->
        <el-form-item label="店主姓名">
          <el-input v-model="detail.shopownerName" />
        </el-form-item>
        <el-form-item label="店主手机号">
          <el-input v-model="detail.shopownerPhone" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="cancel">返回</el-button>
        </el-form-item>
      </el-form>
    </el-scrollbar>
  </section>
</template>
<script>
import { shopAdd, shopUpdate, shopDetail, housePropertyList } from '@/api/api'
export default {
  data() {
    return {
      fileList: [],
      enterpriseIdList:[],
      detail: {
        'account': '',
        'address': '',
        'enterpriseId': '',
        'license': '',
        'logo': '',
        'name': '',
        'serviceScope': '',
        'shopownerName': '',
        'shopownerPhone': ''
      }
    }
  },
  created() {
    const params = {
      curPage: 1,
      pageSize: 9999,
      name: ''
    }
    housePropertyList(params).then(({ data }) => {
      this.enterpriseIdList = data.records
      // this.list = data
    })
    if (this.$route.query.id) {
      shopDetail(this.$route.query.id).then(({ data }) => {
        this.detail = data
        this.fileList[0] = this.detail.logo
      })
    }
  },
  methods: {
    handleRemove(index) {
      this.fileList.splice(index, 1)
    },
    // 上传图片之前的验证
    handleUpload() {
    },
    // 上传成功之后的回调
    handleSuccess(res) {
      if (res.code === 200) {
        this.fileList.push(res.data)
        this.detail.logo = this.fileList[0]
      }
    },
    // 添加/编辑
    submit() {
      // 验证不通过则不提交
      if (!this.vertify()) {
        return false
      }
      if (this.$route.query.id) {
        // 编辑
        shopUpdate(this.detail).then(data => {
          Is.tip('编辑成功', 2000, 'success')
          this.$router.back()
        })
      } else {
        // 新增
        shopAdd(this.detail).then(data => {
          Is.tip('添加成功', 2000, 'success')
          this.$router.back()
        })
      }
    },
    // 返回
    cancel() {
      this.$router.back()
    },
    vertify() {
      // if (Is.empty(this.detail.title)) {
      //   Is.tip('请输入标题')
      //   return false
      // }
      return true
    }
  }}
</script>
